#include "debugdraw.h"
#include "math/vertex.h"
#include "irenderer.h"
//#include "threading/thread.h"

namespace gecko
{

DebugDraw::DebugDraw()
: m_CurrentBuffer(0)
{

}

void DebugDraw::Register( IRenderer* renderer )
{
	m_Renderer = renderer;
	m_CurrentBuffer = 0;
}

void DebugDraw::Line3D( const Vector3f& start, const Vector3f& end, const Colour& startColour, const Colour& endColour )
{
	DebugLine line;
	line.Start = start;
	line.End = end;
	line.StartColour = startColour;
	line.EndColour = endColour;

	m_DebugLineBuffer[m_CurrentBuffer].PushBack(line);
}

void DebugDraw::SwapBuffers()
{
	m_CritialSection.Begin();
		
	m_CurrentBuffer = !m_CurrentBuffer;
	m_DebugLineBuffer[m_CurrentBuffer].Clear();

	m_CritialSection.End();
}

void DebugDraw::Lock(DebugLineBuffer& lineBuffer)
{
	m_CritialSection.Begin();
	lineBuffer = m_DebugLineBuffer[!m_CurrentBuffer];
}

void DebugDraw::Unlock()
{
	m_CritialSection.End();
}

bool DebugDraw::NeedsRendering()
{
	return !m_DebugLineBuffer[!m_CurrentBuffer].Empty();
}


void DebugDrawManager::Register( IRenderer* renderer )
{
	for ( int i = 0; i < MAX_DEBUGDRAWERS; i++ )
	{
		m_DebugDrawers[i].Register(renderer);
	}
}

void DebugDrawManager::Line3D( const Vector3f& start, const Vector3f& end, const Colour& startColour, const Colour& endColour )
{
	unsigned int threadID = 0;//GetThreadIndex();
	m_DebugDrawers[threadID].Line3D(start, end, startColour, endColour);
}

void DebugDrawManager::SwapBuffers()
{
	unsigned int threadID = 0;//GetThreadIndex();
	m_DebugDrawers[threadID].SwapBuffers();
}

DebugDraw* DebugDrawManager::GetDrawer(unsigned int id)
{
	return &m_DebugDrawers[id];
}

unsigned int DebugDrawManager::GetNumDrawers()
{
	return MAX_DEBUGDRAWERS;
}

} // namespace gecko